package com.xuxin.baby.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.utils.ACache;

import java.util.List;

import butterknife.BindView;

/**
 * 欢迎页
 */
public class SplashActivity extends BaseActivity {
    @BindView(R.id.splash_image)
    ImageView splash_image;
    private String memberId;//用户id
    private String tenantId;//租户id
    private String token;//授权token
    private boolean login;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    public void initView() {
        LogUtils.e("onCreate");
        GlideApp.with(this).load(R.mipmap.img_splash).centerCrop().into(splash_image);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        login = aCache.getAsBoolean(BaseConstant.login, false);
        permissions();
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onClick(View view) {
    }

    private final Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 0) {
                if (!login || StringUtils.isEmpty(memberId) ||
                        StringUtils.isEmpty(tenantId) ||
                        StringUtils.isEmpty(token)) {
                    ActivityUtils.startActivity(LoginActivity.class);
                } else {
                    ActivityUtils.startActivity(MainActivity.class);
                }
                finish();
            }
            return false;
        }
    });

    /**
     * 申请读写权限
     */
    public String[] permission = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE};

    private void permissions() {
        PermissionUtils.permissionGroup(permission).callback(new PermissionUtils.FullCallback() {
            @Override
            public void onGranted(@NonNull List<String> granted) {
                mHandler.sendEmptyMessageDelayed(0, 1000);
            }

            @Override
            public void onDenied(@NonNull List<String> deniedForever, @NonNull List<String> denied) {
                ToastUtils.showShort("权限已被拒绝，请打开权限");
                finish();
            }
        }).request();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtils.e("onNewIntent");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.e("onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.e("onResume");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.e("onDestroy");
    }
}